/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javateknik.portal.web.controller;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javateknik.portal.model.entities.Message;
import javateknik.portal.model.entities.MessageDAO;

/**
 *
 * @author hanssonke
 */
@ManagedBean(name = "messageManager")
@SessionScoped
public class MessageManager {

    private DataModel<Message> messages = null;
    private List<DataModel<Message>> pagedMessageList = null;
    private List<Message> messageList = null;
    private Message currentMessage = null;
    private int currentPage = 0;
    private static final int PAGE_SIZE = 7;
    private int pageCount = 0;
    private MessageDAO messageDAO = null;

    public String viewMessageAction() {
        setCurrentMessage(getMessages().getRowData());
        return "detail?faces-redirect=true";
    }

    public String prevPageAction() {
        messages = getPrevPage();
        return null;
    }

    public String nextPageAction() {
        messages = getNextPage();
        return null;
    }

    public String updateMessageAction() {
        messageList.get(messageList.indexOf(getCurrentMessage())).setContent(getCurrentMessage().getContent());
        FacesContext.getCurrentInstance().addMessage("test", new FacesMessage("Content uppdated"));
        getMessageDAO().saveMessage(getCurrentMessage());
        return null;
    }

    public String createMessageAction() {
        messageList.get(messageList.indexOf(getCurrentMessage())).setContent(getCurrentMessage().getContent());
        FacesContext.getCurrentInstance().addMessage("test", new FacesMessage("Content uppdated"));
        getMessageDAO().saveMessage(new Message("Jag", getCurrentMessage().getSubject(), getCurrentMessage().getContent()));
        return "show?faces-redirect=true";
    }

    public String refreshMessageListAction() {
        messageList = getMessageDAO().getMessageList();

        if(messageList == null || messageList.isEmpty())
            return null;

        setCurrentMessage(messageList.get(0));
        createPagedList(messageList);
        messages = getFirstPage();
        return null;
    }

    @PostConstruct
    public void construct() {
        init();
    }

    @PreDestroy
    public void destroy() {
        messages = null;
        if (null != messageList) {
            messageList.clear();
            messageList = null;
        }
        currentMessage = null;
    }

    public void init() {
        refreshMessageListAction();
    }

    private DataModel<Message> getLastPage() {
        currentPage = pageCount;
        return getCurrentPage();
    }

    private DataModel<Message> getFirstPage() {
        currentPage = 0;
        return getCurrentPage();
    }

    private DataModel<Message> getNextPage() {
        if (currentPage + 1 <= pageCount) {
            currentPage++;
        }
        return getCurrentPage();
    }

    private DataModel<Message> getPrevPage() {
        if (currentPage - 1 >= 0) {
            currentPage--;
        }
        return getCurrentPage();
    }

    private void createPagedList(List<Message> messageList) {
        pagedMessageList = new ArrayList<DataModel<Message>>();
        pageCount = 0;
        for (int i = 0; i < messageList.size(); i += PAGE_SIZE) {
            pagedMessageList.add(new ListDataModel<Message>(messageList.subList(i, i + PAGE_SIZE <= messageList.size() ? i + PAGE_SIZE : messageList.size())));
            pageCount++;
        }
    }

    public Message getCurrentMessage() {
        return currentMessage;
    }

    public void setCurrentMessage(Message currentMessage) {
        this.currentMessage = currentMessage;
    }

    public List<Message> getMessageList() {
        return messageList;
    }

    public void setMessageList(List<Message> messageList) {
        this.messageList = messageList;
    }

    public DataModel<Message> getMessages() {
        return messages;
    }

    public void setMessages(DataModel<Message> messages) {
        this.messages = messages;
    }

    private DataModel<Message> getCurrentPage() {
        return pagedMessageList.get(currentPage);
    }

    private MessageDAO getMessageDAO() {
        if (messageDAO == null) {
            messageDAO = new MessageDAO();
        }
        return messageDAO;
    }

    public void setMessageDAO(MessageDAO messageDAO) {
        this.messageDAO = messageDAO;
    }
}
